<?php
namespace Agent\Controller;
use Think\Controller;
class RegController extends Controller {
    public function login(){
        $this->display();
    }
	
	//测试android网络调用
	public function test1(){
		$param = I('param.','','trim');
		file_put_contents('./log/test1.log',date('Y-m-d H:i:s').var_export($param,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$this->ajaxReturn($param);
	}
	
    public function verify(){
		ob_clean();
        $config =    array(
            'fontSize'    =>    12,
            'length'      =>    3,
            'useNoise'    =>    false,
            'bg'=>array(229,229,229),
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }
    public function verify_check($code,$id){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
    public function loginCheck($username,$password,$verify){
        if(!$this->verify_check($verify)){
            alert(0,'验证码错误');
        }
        if($username==''){
            alert(0,'用户名不能为空');
        }
        if(!mobile($username)){
            alert(0,'手机号码错误');
        }
        if($password==''){
            alert(0,'密码不能为空');
        }
        $where['username']=$username;
        $result=M('DistributionPeople')->where($where)->find();
		if($result['status']==0){
			alert(0,'正在审核！');
		}
		if($result['status']==2){
			alert(0,'未通过审核！');
		}
        if(count($result)==0){
            alert(0,'用户名不存在');
        }
        if($result['password']==md5($password. $result['salt'])){
            $result['db']=C('DB_PREFIX_T').'distribution';
            $result['type']=1;
            $result['agent_id']=$result['id'];
            session('agent',$result);
            alert(1,'登录成功');
        }else{
            alert(0,'密码错误');
        }
    }
    public function register(){
		// dump(session());
        $this->display();
    }
    public function exits(){
        session('agent',null);
        session('user_root',null);
        $this->redirect('Reg/login');
    }
    public function registerD($company,$username,$pwd,$pwd2,$name,$email){

        if($company==''){
            alert(0,'用户名不能为空');
        }
        if($username==''){
            alert(0,'用户名不能为空');
        }
        if($pwd==''){
            alert(0,'密码不能为空');
        }
        if($pwd2!=$pwd){
            alert(0,'两次密码不相同');
        }
        if($email==''){
            alert(0,'E-mail不能为空');
        }
        if($name==''){
            alert(0,'姓名不能为空');
        }
        if(!email($email)){
            alert(0,'邮箱地址错误');
        }
        if(!mobile($username)){
            alert(0,'手机号码错误');
        }
        if(M("user")->where("username='".$username."'")->count()>=1){
            alert(0,'用户名已经存在！');
        }
        if(M("user")->where("company='".$company."'")->count()>=1){
            alert(0,'公司名称已经存在！');
        }
        if(M("user")->where("email='".$email."'")->count()>=1){
            alert(0,'电子邮箱已经存在！');
        }
		// $member=I('member');
        $data['salt']=rand(10000,99999);
        $data['username']=$username;
		// $data['member']=$member;
        $data['company']=$company;
        $data['password']=md5($pwd.$data['salt']);
        $data['name']=$name;
        $data['email']=$email;
		$data['status']=0;
		$data['warehouse_code']="T".rand(1000000,9999999);//虚拟仓仓库编码
        $result=M("user")->add($data);
        if($result){
            if($this->createDb(C('DB_PREFIX_T').$username)){
                alert(1,'注册成功');
            }else{
                alert(0,'注册失败，请重试');
            }
        }else{
            alert(0,'注册失败，请重试');
        }
    }
	//username数据库名称
    // public function createDb($username){	
        // $m=M();
        // $f=C('DB_PREFIX_T');
        // $db_name=array('arrival_order','bill','bill_cate','bind_list_bill','book','category','coin','config','contacts_company','cost','country','department','factory_rule','inbound_plan','inbound_plan_detail','level_rule','list','list_cost','list_product','massage','personnel','product','product_order','product_order_byproduct','product_order_detail','pushorder_salesman_cost','pushorder_salesman','pushordergoods_salesman','role','root','section','unit');
        // $m->execute("CREATE DATABASE ".$username);
        // foreach($db_name as $v){
            // $m->execute("CREATE TABLE ".$username.".".$f.$v." LIKE ".$f."main.".$f.$v);
            // if($v=='category' || $v=='root' || $v=='config'){
                // $m->execute("INSERT INTO ".$username.".".$f.$v." SELECT * FROM ".$f."main.".$f.$v);
            // }
        // }
		// $sql_interfaces="INSERT INTO ".$username.".".$f."interfaces SELECT * FROM ".$f."interfaces where warehouse_id=30";
        // return true;
    // }
	//创建数据库
	//$username 数据库名称
	public function createDb($username){

		//创建数据库 事务会自动提交
		$dbName=$username;//数据库名称 "tzwms_+手机号"		
		$Model = M();
		$sql_head="SELECT * FROM information_schema.SCHEMATA where SCHEMA_NAME='$dbName';";
		$ress = $Model->query($sql_head);
		if(!empty($ress)){
			alert(0,'数据库已存在');
		}
		$res2 = $Model->execute('create database ' . $dbName);

		if($res2 === false){
			alert(0,'生成数据失败！');
		}else{
			//使用读取写入方式导入
			require_once('./cloudAPP/Admin/Common/DBManage.class.php');
			$db = new \DBManage(C('DB_HOST'), C('DB_USER'), C('DB_PWD'), $dbName , 'utf8' );
			
			//输出缓存
			ob_start();
			//导入
			$db->restore('./data/erp.sql');//空数据
			$db->restore('./data/erp_config.sql');//插入数据
			//清空缓冲
			ob_end_clean();
			return true;
		}
	}
}